package com.aek.aekdroid;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.widget.RemoteViews;

import com.aek.aekdroid.data.IDbConstants;

public abstract class BaseWidget extends AppWidgetProvider {
	public static final String TAG = "BaseWidget";
	protected int widgetType;
	protected int launcher;

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		long now = System.currentTimeMillis() - 7200000;

		Cursor c = context.getContentResolver().query(MatchProvider.CONTENT_URI, null, "datetime >  ? AND deleted = ?",
				new String[] { String.valueOf(now), "0" }, MatchProvider.SORT_ORDER);
		try {
			if (c.moveToFirst()) {
				MyLog.d(TAG, "Updating widget " + widgetType);
				CharSequence homeTeam = c.getString(c.getColumnIndex(IDbConstants.MATCHES_C_HOME_TEAM));
				CharSequence awayTeam = c.getString(c.getColumnIndex(IDbConstants.MATCHES_C_AWAY_TEAM));
				CharSequence homeGoals = c.getString(c.getColumnIndex(IDbConstants.MATCHES_C_HOME_GOALS));
				CharSequence awayGoals = c.getString(c.getColumnIndex(IDbConstants.MATCHES_C_AWAY_GOALS));
				Long d = c.getLong(c.getColumnIndex(IDbConstants.MATCHES_C_DATETIME));
				CharSequence dateTime = Helper.createDate(d);
				CharSequence channel = c.getString(c.getColumnIndex(IDbConstants.MATCHES_C_CHANNEL));
				for (int appWidgetId : appWidgetIds) {
					MyLog.d(TAG, "Updating widget " + appWidgetId);
					RemoteViews views = new RemoteViews(context.getPackageName(), widgetType);
					views.setTextViewText(R.id.widgetHomeTeam, homeTeam);
					views.setTextViewText(R.id.widgetAwayTeam, awayTeam);
					views.setTextViewText(R.id.widgetHomeGoals, homeGoals);
					views.setTextViewText(R.id.widgetAwayGoals, awayGoals);
					views.setTextViewText(R.id.widgetDatetime, dateTime);
					String img = channel.toString().replaceAll(" ", "").toLowerCase();
					if (img.equals("-")) {
						img = "notv";
					}
					int r = context.getResources().getIdentifier(img, "drawable", "com.aek.aekdroid");
					views.setImageViewResource(R.id.widgetChannel, r);
					PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ScheduleActivity.class), 0);
					views.setOnClickPendingIntent(launcher, pi);
					appWidgetManager.updateAppWidget(appWidgetId, views);
					MyLog.d(TAG, "Widget updated");
				}
			} else {
				MyLog.d(TAG, "No updates");
				for (int appWidgetId : appWidgetIds) {
					RemoteViews views = new RemoteViews(context.getPackageName(), widgetType);
					views.setTextViewText(R.id.widgetHomeTeam, context.getString(R.string.noMatch));
					views.setTextViewText(R.id.widgetAwayTeam, context.getString(R.string.noMatch2));
					views.setTextViewText(R.id.widgetHomeGoals, "");
					views.setTextViewText(R.id.widgetAwayGoals, "");
					views.setTextViewText(R.id.widgetDatetime, "");
					int r = context.getResources().getIdentifier("notv", "drawable", "com.aek.aekdroid");
					views.setImageViewResource(R.id.widgetChannel, r);
					PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ScheduleActivity.class), 0);
					views.setOnClickPendingIntent(launcher, pi);
					appWidgetManager.updateAppWidget(appWidgetId, views);

				}
			}
		} finally {
			c.close();
		}
	}

}
